package com.fuyu.common.web.controller;

import com.fuyu.common.model.R;
import com.fuyu.common.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

@Getter
@Validated
public class BaseController {

    @Autowired
    private HttpServletRequest httpServletRequest;


    public <T> R<T> success(T data) {
        return R.success(data);
    }


    public <T> R<T> crud(boolean result) {
        return R.crud(result);
    }


    /**
     * 获取请求tou
     */
    public String getRequestToken() {
        return httpServletRequest.getHeader("Authorization");
    }

    /**
     * 解析token 获取用户id
     */
    public String getUserId() {
        String requestToken = getRequestToken();
        if (requestToken == null) {
            throw new IllegalStateException();
        }
        return JwtUtil.getUserId(requestToken);
    }

}
